package com.example.paopao.consumer.worker.service.impl;


import com.example.paopao.consumer.worker.dao.persist.mapper.BusinessMapper;
import com.example.paopao.consumer.worker.pojo.vo.BusinessVO;
import com.example.paopao.consumer.worker.pojo.vo.DistrictInfoVO;
import com.example.paopao.consumer.worker.pojo.vo.WorkVO;
import com.example.paopao.consumer.worker.service.IBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessServiceImpl implements IBusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public List<BusinessVO> list() {
        List<BusinessVO> list = businessMapper.list();

        List<WorkVO> workName = businessMapper.getWorkName();

        List<DistrictInfoVO> districtName = businessMapper.getDistrictName();

        Integer dPeconomizeId = null;
        Integer dMarketId = null;
        Integer dDisitingishId = null;

        Integer pPeconomizeId = null;
        Integer pMarketId = null;
        Integer pDisitingishId = null;

        for (int i = 0; i < list.size(); i++) {
            dPeconomizeId = list.get(i).getDPeconomizeId();
            dMarketId =  list.get(i).getDMarketId();
            dDisitingishId =  list.get(i).getDDisitingishId();
            pPeconomizeId =  list.get(i).getPPeconomizeId();
            pMarketId =  list.get(i).getPMarketId();
            pDisitingishId =  list.get(i).getPDisitingishId();

            StringBuffer stringBuilder1 = new StringBuffer();
            for (DistrictInfoVO districtInfoVO : districtName) {
                Integer id = districtInfoVO.getId();
                if (dPeconomizeId.equals(id)) {
                    stringBuilder1.append(districtInfoVO.getDistrict());
                }
                if (dMarketId.equals(id)) {
                    stringBuilder1.append(districtInfoVO.getDistrict());
                }
                if (dDisitingishId.equals(id)) {
                    stringBuilder1.append(districtInfoVO.getDistrict());
                }
            }
            list.get(i).setDisworkName(stringBuilder1.toString());


            StringBuffer stringBuilder2 = new StringBuffer();
            for (DistrictInfoVO districtInfoVO : districtName) {
                Integer id = districtInfoVO.getId();
                if (pPeconomizeId.equals(id)) {
                    stringBuilder2.append(districtInfoVO.getDistrict());
                }
                if (pMarketId.equals(id)) {
                    stringBuilder2.append(districtInfoVO.getDistrict());
                }
                if (pDisitingishId.equals(id)) {
                    stringBuilder2.append(districtInfoVO.getDistrict());
                }
            }
            list.get(i).setPredistName(stringBuilder2.toString());
        }




        for (BusinessVO businessVO : list) {
            for (WorkVO workVO : workName) {
                if (businessVO.getId() == workVO.getRinfoId()) {
                    businessVO.getList().add(workVO);
                }
            }
        }

        return list;
    }

    @Override
    public List<BusinessVO> searchByWd(String wd) {

        System.out.println(wd);

        List<BusinessVO> list = businessMapper.selectByWd(wd);

        List<WorkVO> workName = businessMapper.getWorkName();

        List<DistrictInfoVO> districtName = businessMapper.getDistrictName();

        Integer dPeconomizeId = null;
        Integer dMarketId = null;
        Integer dDisitingishId = null;

        Integer pPeconomizeId = null;
        Integer pMarketId = null;
        Integer pDisitingishId = null;


        for (int i = 0; i < list.size(); i++) {
            dPeconomizeId = list.get(i).getDPeconomizeId();
            dMarketId =  list.get(i).getDMarketId();
            dDisitingishId =  list.get(i).getDDisitingishId();
            pPeconomizeId =  list.get(i).getPPeconomizeId();
            pMarketId =  list.get(i).getPMarketId();
            pDisitingishId =  list.get(i).getPDisitingishId();

            StringBuffer stringBuilder = new StringBuffer();
            for (DistrictInfoVO districtInfoVO : districtName) {
                Integer id = districtInfoVO.getId();
                if (dPeconomizeId.equals(id)) {
                    stringBuilder.append(districtInfoVO.getDistrict());
                }
                if (dMarketId.equals(id)) {
                    stringBuilder.append(districtInfoVO.getDistrict());
                }
                if (dDisitingishId.equals(id)) {
                    stringBuilder.append(districtInfoVO.getDistrict());
                }
            }
            list.get(i).setDisworkName(stringBuilder.toString());


            StringBuffer stringBuilder1 = new StringBuffer();
            for (DistrictInfoVO districtInfoVO : districtName) {
                Integer id = districtInfoVO.getId();
                if (pPeconomizeId.equals(id)) {
                    stringBuilder1.append(districtInfoVO.getDistrict());
                }
                if (pMarketId.equals(id)) {
                    stringBuilder1.append(districtInfoVO.getDistrict());
                }
                if (pDisitingishId.equals(id)) {
                    stringBuilder1.append(districtInfoVO.getDistrict());
                }
            }
            list.get(i).setPredistName(stringBuilder.toString());
        }




        for (BusinessVO businessVO : list) {
            for (WorkVO workVO : workName) {
                if (businessVO.getId() == workVO.getRinfoId()) {
                    businessVO.getList().add(workVO);
                }
            }
        }

        System.out.println(list);
        return list;
    }
}
